package com.lb.jwitter.action;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhi;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;

import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXADonXinGiaHanHocPhiAddFormAction{
    private int id; 
    public void setId(int id) {
       this.id = id;
 }

    public int getId() {
       return id;
 }

    
    private String mSHV;
    private String hoTen;
    private int hocVienId;
    private Long tuHocKyId;
    private Date today;
    
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;
    private TblQLDTTXAHocVien tblQLDTTXAHocVien;
    
    
    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;
    private List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocs;
    
    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private List<TblCanBo> tblCanBos; 
    public List<TblCanBo>  getTblCanBos(){ 
              return tblCanBos; 
     } 

    private TblCanBoService tblCanBoService; 

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 


    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXAHocVienHocPhi> tblQLDTTXAHocVienHocPhis; 
    public List<TblQLDTTXAHocVienHocPhi>  getTblQLDTTXAHocVienHocPhis(){ 
              return tblQLDTTXAHocVienHocPhis; 
     } 

    private TblQLDTTXAHocVienHocPhiService tblQLDTTXAHocVienHocPhiService; 

    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_DonXinGiaHanHocPhi", "them")){ 
        return "noprivilege"; 
    }        
    
    	tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 
        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 
        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
	    

        tblCanBos = tblCanBoService.findAlltblCanBo(0, 0);

        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam(0, 0);

        

	    today = new Date();

        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        if(tuHocKyId !=null && tuHocKyId != 0){
        	tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(tuHocKyId);
        } else {

        	cons.clear();
            

            	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
            	ConditionReport con = new ConditionReport(); 
                con.setCol("thoiGianBatDau");
                con.setValue1(df.format(today));
                con.setOperator("le");
                con.setType("date");
                cons.add(con);	
                
                ConditionReport con2 = new ConditionReport(); 
                con2.setCol("thoiGianKetThuc");
                con2.setValue1(df.format(today));
                con2.setOperator("ge");
                con2.setType("date");
                cons.add(con2);	

            tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

            if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() > 0){
            	tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
            }
        	
        }
        
        cons.clear();
        if(mSHV != null && !mSHV.equalsIgnoreCase("")){
        	ConditionReport con = new ConditionReport();
        	con.setCol("MSHV");
        	con.setOperator("eq");
        	con.setValue1(mSHV);
        	
        	cons.add(con);
        	
        	List<TblQLDTTXAHocVien> tblHocVienList = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 1, 1);
        	
        	if(tblHocVienList != null && tblHocVienList.size() > 0)
        		tblQLDTTXAHocVien = tblHocVienList.get(0);
        }
        
        if(tblQLDTTXAHocVien != null && tblQLDTTXAHocKyTrongNam != null){
        	cons.clear();
        	if(true){
            	ConditionReport con = new ConditionReport(); 
                con.setCol("hocVienHocPhiId.hocVienId.id");
                con.setValue1(tblQLDTTXAHocVien.getId());
                con.setOperator("eq");
                cons.add(con);		 
        	}
        	if(true){
            	ConditionReport con = new ConditionReport(); 
                con.setCol("hocVienHocPhiId.hocKyId.id");
                con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
                con.setOperator("eq");
                cons.add(con);		 
        	}
			if(true){
            	ConditionReport con = new ConditionReport(); 
                con.setCol("daDongHP");
                con.setValue1(false);
                con.setOperator("eq");
                cons.add(con);		 
        	}
			if(true){
            	ConditionReport con = new ConditionReport(); 
                con.setCol("mienMon");
                con.setValue1(false);
                con.setOperator("eq");
                cons.add(con);		 
        	}
            tblQLDTTXAHocVienHocPhiMonHocs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
        }
            
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam( 0, 0);

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi AddForm tbl_QLDTTXA_DonXinGiaHanHocPhi Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
	return tblQLDTTXAHocKyTrongNams;
}

public void setTblQLDTTXAHocKyTrongNams(
		List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams) {
	this.tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNams;
}

public String getMSHV() {
	return mSHV;
}

public void setMSHV(String mshv) {
	mSHV = mshv;
}

public String getHoTen() {
	return hoTen;
}

public void setHoTen(String hoTen) {
	this.hoTen = hoTen;
}

public int getHocVienId() {
	return hocVienId;
}

public void setHocVienId(int hocVienId) {
	this.hocVienId = hocVienId;
}


public Date getToday() {
	return today;
}

public void setToday(Date today) {
	this.today = today;
}

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
}

public void setTblQLDTTXAHocKyTrongNam(
		TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
	this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
}

public TblQLDTTXAHocVien getTblQLDTTXAHocVien() {
	return tblQLDTTXAHocVien;
}

public void setTblQLDTTXAHocVien(TblQLDTTXAHocVien tblQLDTTXAHocVien) {
	this.tblQLDTTXAHocVien = tblQLDTTXAHocVien;
}

public List<TblQLDTTXAHocVienHocPhiMonHoc> getTblQLDTTXAHocVienHocPhiMonHocs() {
	return tblQLDTTXAHocVienHocPhiMonHocs;
}

public void setTblQLDTTXAHocVienHocPhiMonHocs(
		List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocs) {
	this.tblQLDTTXAHocVienHocPhiMonHocs = tblQLDTTXAHocVienHocPhiMonHocs;
}

public Long getTuHocKyId() {
	return tuHocKyId;
}

public void setTuHocKyId(Long tuHocKyId) {
	this.tuHocKyId = tuHocKyId;
} 
 } 
 
